package cn.tedu.tenengdong.controller;


import cn.tedu.tenengdong.mapper.ProductMapper;
import cn.tedu.tenengdong.pojo.dto.ProductDTO;
import cn.tedu.tenengdong.pojo.dto.ProductUpdateDTO;
import cn.tedu.tenengdong.pojo.entity.Product;
import cn.tedu.tenengdong.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Value("${imagesPath}")
    private String dirPath;
    @Autowired(required = false)
    ProductMapper mapper;
    @RequestMapping("insert")
    public void insert(@RequestBody ProductDTO productDTO){
        System.out.println("productDTO = " + productDTO);
        Product p = new Product();
        BeanUtils.copyProperties(productDTO,p);
        p.setCreated(new Date());
        mapper.insert(p);
    }
    @RequestMapping("selectAdmin")
    public List<ProductAdminVO> selectAdmin(){
        return mapper.selectAdmin();
    }
    @RequestMapping("delete")
    public void delete(int id){
        //通过id查询图片路径 并删除文件
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        //删除数据库中数据
        mapper.deleteById(id);

    }
    @RequestMapping("selectUpdateById")
    public ProductUpdateVO selectUpdateById(int id){
        return mapper.selectUpdateById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDTO productUpdateDTO){
        System.out.println("productUpdateDTO = " + productUpdateDTO);
        //得到原来的图片路径
        String url = mapper.selectUrlById(productUpdateDTO.getId());
        //判断如果新路径和原来路径不相等则删除原来路径对应的图片
        if (!url.equals(productUpdateDTO.getUrl())){
            new File(dirPath+url).delete();
        }
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO,product);

        mapper.update(product);
    }
    @RequestMapping("selectTop")
    public List<ProductTopVO>selectTop(){

        return mapper.selectTop();
    }
    @RequestMapping("selectIndex")
    public List<ProductIndexVO> selectIndex(){
        return mapper.selectIndex();
    }
    //  id=5
    @RequestMapping("selectDetailById")
    public ProductDetailVO selectDetailById(int id, HttpSession session){
        //第一次从Session里面通过id获取时是获取不到内容的
        String info = (String) session.getAttribute("view"+id);
        if (info==null){//info为null代表之前没有浏览过
            //让商品的浏览量+1
            mapper.updateViewCountById(id);
            //把浏览过的id保存到Session里面去
            session.setAttribute("view"+id,"visited");
        }
        return mapper.selectDetailById(id);
    }
    @RequestMapping("selectByCid")
    public List<ProductIndexVO> selectByCid(int id){
        return mapper.selectByCid(id);
    }
    @RequestMapping("selectByWd")
    public List<ProductIndexVO> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }
}
